package com.broadtext.message.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.broadtext.common.bean.EntityPageBean;
import com.broadtext.common.utils.StringUtils;
import com.broadtext.message.model.MsgNoticeConfig;
import com.broadtext.message.service.IEmailService;
import com.broadtext.message.service.IMsgNoticeConfigService;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * <p>ClassName: MessageAction</p>
 * <p>Description: 消息控制层</p>
 * <p>Author: zhanghailin</p>
 * <p>Date: 2018-2-23</p>
 */
@Controller
@RequestMapping("/main/msg/notice")
public class MessageConfigurationAction {

    @Autowired
    @Qualifier("msgNoticeConfigService")
    private IMsgNoticeConfigService msgNoticeConfigService;

    @Autowired
    @Qualifier("emailService")
    private IEmailService emailService;

    /**
     *
     */
    @RequestMapping(value = "/getNoticeConfig.mvc")
    @ResponseBody
    public Map<String,Object> getMessage(HttpServletRequest request, HttpServletResponse response)  {
        PageBounds pageBounds = new PageBounds();
        //解析请求参数，将String字符串转为map
        Map<String,Object> map = new HashMap<String,Object>();
        String aoData = request.getParameter("aoData");
        JSONArray array = JSONArray.parseArray(aoData);
        if (array!=null) {
            Iterator<Object> it = array.iterator();
            while (it.hasNext()) {
                JSONObject obj;
                obj = (JSONObject) it.next();
                if (obj.get("value") != null && StringUtils.isNotBlank(obj.get("value")+"")) {
                    map.put(obj.get("name") + "", obj.get("value"));
                }
            }
        }
        String receiver = String.valueOf(request.getSession().getAttribute("sys.user"));
        Map<String, Object> dataMap = new HashMap<String, Object>();
        if(StringUtils.isBlank(receiver)) {
            return dataMap;
        }
        map.put("receiver", receiver);
        //分页查询条数
        pageBounds.setLimit(Integer.parseInt(map.get("iDisplayLength").toString()));
        //分页第几页
        pageBounds.setPage(Integer.parseInt(map.get("iDisplayStart").toString()) / pageBounds.getLimit() + 1);
        try {
            EntityPageBean<MsgNoticeConfig> pageBean = this.msgNoticeConfigService.getMsgNoticeConfigPageBean(map, pageBounds);
            dataMap.put("recordsTotal", pageBean.getiTotalRecords());
            dataMap.put("recordsFiltered", pageBean.getiTotalRecords());
            dataMap.put("aaData", pageBean.getResults());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataMap;
    }

    @RequestMapping(value = "/saveMsgNoticeConfig.mvc")
    @ResponseBody
    public Map<String, Object> saveMsgNoticeConfig(@RequestBody MsgNoticeConfig msgNoticeConfig, HttpServletRequest request) {
        Map<String, Object> dataMap = new HashMap<>();
        String operator = String.valueOf(request.getSession().getAttribute("sys.userName"));
        try {
            this.msgNoticeConfigService.saveMsgNoticeConfig(msgNoticeConfig,operator);
            dataMap.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            dataMap.put("result",false);
        }
        return dataMap;
    }

    @RequestMapping(value = "/getMsgNoticeConfig.mvc")
    @ResponseBody
    public Map<String, Object> saveMsgNoticeConfig(String receiver) {
        Map<String, Object> dataMap = new HashMap<>();
        try {
            MsgNoticeConfig msgNoticeConfig = this.msgNoticeConfigService.getMsgNoticeConfig(receiver);
            dataMap.put("data",msgNoticeConfig);
            dataMap.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            dataMap.put("result",false);
        }
        return dataMap;
    }

    @RequestMapping(value = "/sendEmail.mvc")
    @ResponseBody
    public void sendEmail() {
        try {
            this.emailService.sendEmail();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
